package com.th.supcom.pds.common.client.widget;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreFilter;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridView;
import com.extjs.gxt.ui.client.widget.grid.GridViewConfig;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

/**
 * PDS Grid
 * 
 * @author yangchong
 * @param <M>
 */
public abstract class PdsGrid <M extends ModelData> extends ContentPanel
{
    private EditorGrid <M> m_grid;
    private ListStore <M> m_listStore;
    private CheckBoxSelectionModel <M> m_sm;
    private StoreFilter <M> m_storeFilter;
    
    /**
     * 默认构造函数
     */
    public PdsGrid ()
    {
        initUI ();
        createGrid ();
        createFilter ();
    }

    /**
     * 清除数据
     */
    public void clear ()
    {
        m_listStore.removeAll ();
    }

    /**
     * 创建Column Model
     * 
     * @return
     */
    public abstract ColumnModel createColumnModel ();

    /**
     * 创建过滤器
     */
    private void createFilter ()
    {
        m_storeFilter = new StoreFilter <M> ()
        {
            @Override
            public boolean select (final Store <M> store, final M parent, final M item, final String property)
            {
                return false;
            }
        };
    }

    /**
     * 创建Grid
     */
    private void createGrid ()
    {
        m_listStore = new ListStore <M> ();
        m_sm = new CheckBoxSelectionModel <M> ();
        m_grid = new EditorGrid <M> (m_listStore, createColumnModel ());
        m_grid.setBorders (false);
        m_grid.setLoadMask (true);
        m_grid.setColumnLines (true);
        add (m_grid);
    }

    /**
     * 过滤
     */
    public void filter ()
    {
        m_listStore.clearFilters ();
        m_listStore.addFilter (m_storeFilter);
        m_listStore.applyFilters ("");
    }

    /**
     * 获取单选框
     * 
     * @return
     */
    public CheckBoxSelectionModel <M> getCheckBoxSelectionModel ()
    {
        return m_sm;
    }

    /**
     * 获取Grid
     * 
     * @return
     */
    public Grid <M> getGrid ()
    {
        return m_grid;
    }

    /**
     * 获取listStore
     * 
     * @return
     */
    public ListStore <M> getListStore ()
    {
        return m_listStore;
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        setLayout (new FitLayout ());
        setHeaderVisible (false);
        setBorders (false);
        setBodyBorder (false);
    }

    /**
     * Grid加载的时候显示的消息
     */
    public void maskMsg (final String message)
    {
        m_grid.mask (message);
    }

    /**
     * 设置GridView
     * 
     * @param gridView
     */
    public void setGridView (final GridView gridView)
    {
        m_grid.setView (gridView);
    }

    /**
     * 设置ViewConfig
     * 
     * @param view
     */
    public void setViewConfig (final GridViewConfig view)
    {
        m_grid.getView ().setViewConfig (view);
    }

    /**
     * Grid加载完的时候结束显示的消息
     */
    public void unmaskMsg ()
    {
        m_grid.unmask ();
    }
}
